﻿using System;
using System.IO;
using System.Globalization;
using System.Configuration;


    public class ErrorHandler
    {
        /// Handles error by accepting the error message
        /// Displays the page on which the error occured
        public static void WriteError(Exception p_objException)
        {
            try
            {
                string path = ConfigurationManager.AppSettings.Get("ErrorLogLocation") + DateTime.Today.ToString("dd-MM-yyyy") + ".log";
                if (!File.Exists(path))
                {
                    File.Create(path).Close();
                }
                using (StreamWriter w = File.AppendText(path))
                {
                    string strUserInfo = null;
                    if (System.Web.HttpContext.Current.Session["RegistrationID"] != null &&
                        System.Web.HttpContext.Current.Session["Name"] != null)
                    {
                        strUserInfo = System.Web.HttpContext.Current.Session["RegistrationID"] + "-" +
                                      System.Web.HttpContext.Current.Session["Name"];
                    }
                    else
                    {
                        strUserInfo = "Anonymous User";
                    }
                    w.WriteLine("Log Entry : {0}", DateTime.Now.ToString(CultureInfo.InvariantCulture));
                    string err = "Error Location : " + System.Web.HttpContext.Current.Request.Url.ToString() +
                                 "\r\nUser Info : " + strUserInfo +          
                                 "\r\nError Message : " + p_objException.Message+
                                  "\r\nStack Trace : " + p_objException.StackTrace;
                    w.WriteLine(err);
                    w.WriteLine("----------------------------------------------------\r\n");
                    w.Flush();
                    w.Close();
                }
            }
            catch (Exception ex)
            {
                WriteError(ex);
            }
        }

        public static void WriteError(Exception p_objException, string p_strErrorLocation, string p_strUserInfo)
        {
            try
            {
                string path = ConfigurationManager.AppSettings.Get("ErrorLogLocation") + DateTime.Today.ToString("dd-MM-yyyy") + ".log";
                if (!File.Exists(path))
                {
                    File.Create(path).Close();
                }
                using (StreamWriter w = File.AppendText(path))
                {
                    w.WriteLine("Log Entry : {0}", DateTime.Now.ToString(CultureInfo.InvariantCulture));
                    string err = "Error Location : " + p_strErrorLocation +
                                  "\r\nUser Info : " + p_strUserInfo +          
                                  "\r\nError Message : " + p_objException.Message +
                                  "\r\nStack Trace :" + p_objException.StackTrace;
                    w.WriteLine(err);
                    w.WriteLine("----------------------------------------------------\r\n");
                    w.Flush();
                    w.Close();
                }
            }
            catch (Exception ex)
            {
                WriteError(ex);
            }

        }

        public static void WriteError(Exception p_objException, string p_strAdditionalMsg)
        {
            try
            {
                string path = ConfigurationManager.AppSettings.Get("ErrorLogLocation") + DateTime.Today.ToString("dd-MM-yyyy") + ".log";
                if (!File.Exists(path))
                {
                    File.Create(path).Close();
                }
                using (StreamWriter w = File.AppendText(path))
                {
                    string strUserInfo = null;
                    if (System.Web.HttpContext.Current.Session["RegistrationID"] != null &&
                        System.Web.HttpContext.Current.Session["Name"] != null)
                    {
                        strUserInfo = System.Web.HttpContext.Current.Session["RegistrationID"] + "-" +
                                      System.Web.HttpContext.Current.Session["Name"];
                    }
                    else
                    {
                        strUserInfo = "Anonymous User";
                    }
                    w.WriteLine("Log Entry : {0}", DateTime.Now.ToString(CultureInfo.InvariantCulture));
                    string err = "Error Location : " + System.Web.HttpContext.Current.Request.Url.ToString() +
                                 "\r\nUser Info : " + strUserInfo +
                                 "\r\nError Message : " + p_objException.Message +
                                 "\r\nAdditional Message : " + p_strAdditionalMsg +
                                  "\r\nStack Trace : " + p_objException.StackTrace;
                    w.WriteLine(err);
                    w.WriteLine("----------------------------------------------------\r\n");
                    w.Flush();
                    w.Close();
                }
            }
            catch (Exception ex)
            {
                WriteError(ex);
            }
        }
    }
